/**
 * date转换为2020/02/03
 */
export function fromDataA(newDate, type) {
    //当前年
    let newDateFullYear = newDate.getFullYear();
    //当前月
    let newDateFullMonth = newDate.getMonth() + 1;
    //格式化月份
    if (newDateFullMonth < 10) {
        newDateFullMonth = "0" + newDateFullMonth;
    }
    //当前日
    let newDateFullDate = newDate.getDate();
    if (type == "m") {
        //年月
        return newDateFullYear + "/" + newDateFullMonth;
    } else if (type == "d") {
        //年月日
        return newDateFullYear + "/" + newDateFullMonth + "/" + newDateFullDate;
    }
}

/*
 *起始时间与结束时间的时间间隔不得低于0分钟，不得超过10分钟
 */
export function isValidTimeInterval(startTimeStr, endTimeStr) {
    console.log("startTimeStr", startTimeStr);
    console.log("endTimeStr", endTimeStr);

    const startTime = new Date(startTimeStr);
    const endTime = new Date(endTimeStr);

    if (isNaN(startTime.getTime()) || isNaN(endTime.getTime())) {
        return false;
    }

    const timeDiff = endTime - startTime;
    const minutesDiff = timeDiff / (1000 * 60);
    console.log("minutesDiff", minutesDiff);
    return minutesDiff >= -10 && minutesDiff <= 10;
}

/**
 *
 * @returns 获取网络时间
 */
export const getTimeFromWorldTimeAPI = async () => {
    try {
        // const response = await fetch("https://worldtimeapi.org/api/timezone/Etc/UTC");
        // const data = await response.json();
        // return new Date(data.unixtime * 1000); // 转换为 Date 对象

        const response = await fetch("https://timeapi.io/api/time/current/zone?timeZone=Europe%2FAmsterdam");
        const data = await response.json();
        // return new Date(data.unixtime * 1000); // 转换为 Date 对象
        const originalDate = new Date(data.dateTime);

        // 增加 6 小时
        const sixHoursInMilliseconds = 6 * 60 * 60 * 1000;
        const newDate = new Date(originalDate.getTime() + sixHoursInMilliseconds);
        console.log("newDate", newDate);

        return newDate; // 转换为 Date 对象
    } catch (error) {
        console.error("Error fetching time:", error);
        return new Date(); // 回退到本地时间
    }
};

/**
 *
 * @param {根据date获取字符串} currentDate
 * @returns
 */
export function GetCurrentTimeStr(currentDate = new Date()) {
    // var currentDate = new Date();
    var year = currentDate.getFullYear().toString();
    var month = (currentDate.getMonth() + 1).toString();
    if (month.length === 1) {
        month = "0" + month;
    }
    var date = currentDate.getDate().toString();
    if (date.length === 1) {
        date = "0" + date;
    }
    var hour = currentDate.getHours().toString();
    if (hour.length === 1) {
        hour = "0" + hour;
    }
    var minute = currentDate.getMinutes().toString();
    if (minute.length === 1) {
        minute = "0" + minute;
    }
    var second = currentDate.getSeconds().toString();
    if (second.length === 1) {
        second = "0" + second;
    }

    return year + "-" + month + "-" + date + " " + hour + ":" + minute + ":" + second;
}

/**
 * date解析年月日数组
 */
export function fromDataArray(newDate) {
    //当前年
    let newDateFullYear = newDate.getFullYear();
    //当前月
    let newDateFullMonth = newDate.getMonth() + 1;
    //格式化月份
    if (newDateFullMonth < 10) {
        newDateFullMonth = "0" + newDateFullMonth;
    }
    //当前日
    let newDateFullDate = newDate.getDate();
    return [newDateFullYear, newDateFullMonth, newDateFullDate];
}

/**
 * 获取前后一天
 */
export function getNextDay(date, day) {
    var dd = new Date(date);
    dd.setDate(dd.getDate() + day);
    return dd;
}

/**
 * 格式转化汉字： 2023-04-23
 */
export function formatDateG(date, type) {
    let m = date.getMonth() + 1;
    if (m < 10) {
        m = "0" + m;
    }

    if (type == "m") {
        //年月
        return `${date.getFullYear()}-${m}`;
    } else if (type == "d") {
        //年月日
        return `${date.getFullYear()}-${m}-${date.getDate()}`;
    }
}

/**
 * 格式转化 2023年04月23日
 */
export function formatDateHanZi(date, type) {
    let m = date.getMonth() + 1;
    if (m < 10) {
        m = "0" + m;
    }

    if (type == "m") {
        //年月
        return `${date.getFullYear()}年${m}月`;
    } else if (type == "d") {
        //年月日
        return `${date.getFullYear()}年${m}月${date.getDate()}日`;
    }
}

/**
 * date转换为2020/02/03
 */
export function fromDataB(date) {
    return dd;
}

/**
 * 格式转化 2023年04月
 */
export function hanFormatDateB(date) {
    return dd;
}
